package com.yue.design.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
 * 使用jdk的proxy进行代理，被代理的对象必须实现接口
 */
public class QianJia implements InvocationHandler {

    private BuyHouse target;

    public BuyHouse getInstance(BuyHouse target){
        this.target = target;
        return (BuyHouse)Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(this.target,args);
        after();
        return obj;
    }
    public void before(){
        System.out.println("我是中介开始找房");
    }
    public void after(){
        System.out.println("我是中介结束找房");
    }
}
